#include "input.h"

#include "JPVec2.h"

Input::Input(Camera *camera):
m_mousex(0),
m_mousey(0)
{
	memset(m_keys, 0, 256);
	memset(m_keyPressOnce, 0, 256);

	m_camera = camera;
}

void Input::release()
{
	m_camera = NULL;
}

void Input::keyboard()
{
	if(m_keys[VK_ESCAPE])
		exit(-1);

	if(m_camera->getMode())
	{
		if(m_keys['W'])
			m_camera->moveForward(4.0f);

		if(m_keys['A'])
			m_camera->strafeCam(4.0f);

		if(m_keys['S'])
			m_camera->moveForward(-4.0f);

		if(m_keys['D'])
			m_camera->strafeCam(-4.0f);
	}

	if(m_keys['1'] && !m_keyPressOnce['1'])
	{
		if(m_camera->getMode())
			m_camera->setMode(THIRD);
		else
			m_camera->setMode(FP);
		m_keyPressOnce['1'] = true;
	}
	if(m_keyPressOnce['1'] && !m_keys['1'])
	{
		m_keyPressOnce['1'] = false;
	}


}

void Input::keyPress(WPARAM wParam)
{
	m_keys[wParam] = true;
}

void Input::keyUp(WPARAM wParam)
{
	m_keys[wParam] = false;
}

void Input::mouseMove(HWND hWnd)
{
	const int middleX = S_WIDTH/2;
	const int middleY = S_HEIGHT/2;

	POINT mouse;	
	GetCursorPos(&mouse);	// Get the 2D mouse cursor (x,y) position
	//ScreenToClient(hWnd, &mouse); // Mouse coords within the window  

	if(m_camera->getMode())
	{    
		// if the mouse hasn't moved, return without doing
		// anything to our view
		if((mouse.x == middleX) && (mouse.y == middleY))
			return;
		else
			// otherwise move the mouse back to the middle of the screen
			SetCursorPos(middleX,middleY);

		JPVec2 mouseDirection;
		mouseDirection.x = ((float)middleX - (float)mouse.x)/50.0f; 
		mouseDirection.y = ((float)middleY - (float)mouse.y)/50.0f;

		m_camera->update(mouseDirection);
	}
	if(!m_camera->getMode())
	{
		if(m_keys[VK_LBUTTON])
		{
			JPVec2 mouseDirection;
			mouseDirection.x = ((float)m_mousex - (float)mouse.x)/50.0f; 
			mouseDirection.y = ((float)m_mousey - (float)mouse.y)/50.0f;

			m_camera->update(mouseDirection);
		}

		if(m_keys[VK_RBUTTON])
		{
			JPVec2 mouseDirection;
			mouseDirection.x = ((float)mouse.x - (float)m_mousex)/5.0f; 
			mouseDirection.y = ((float)mouse.y - (float)m_mousey)/5.0f;

			m_camera->moveForward(mouseDirection.y);
			m_camera->strafeCam(mouseDirection.x);
		}

		if(m_keys[VK_MBUTTON])  
			m_camera->changezoom(2*((float)m_mousey - (float)mouse.y));
	}
	m_mousex = mouse.x;
	m_mousey = mouse.y;
}
